package com.deyang.application;

import android.app.Activity;
import android.app.Application;
import android.content.Context;
import android.graphics.Typeface;

import com.deyang.entity.TokenInfo;
import com.deyang.entity.UserInfo;
import com.deyang.maillist.R;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

import java.util.ArrayList;
import java.util.List;

import es.dmoral.toasty.Toasty;

/**
 * @author 王德阳
 * @data 创建时间: 2017/11/14 0014
 * @Description
 */
public class MyApplication extends Application {

    private static MyApplication application;
    private List<Activity> openActivityList;
    //电影选座
    private List<Activity> openFilmSelectBookActivityList;

    private TokenInfo tokenInfo;
    private UserInfo userInfo;


    @Override
    public void onCreate() {
        super.onCreate();
        application = this;
        openActivityList = new ArrayList<>();
        openFilmSelectBookActivityList = new ArrayList<>();


        Toasty.Config.getInstance()
                .setErrorColor(getResources().getColor(R.color.toast_error))
                .setInfoColor(getResources().getColor(R.color.toast_info))
                .setSuccessColor(getResources().getColor(R.color.toast_success))
                .setWarningColor(getResources().getColor(R.color.toast_warning))
                .setTextColor(getResources().getColor(R.color.textColor_white))
                .tintIcon(true)
                .setToastTypeface(Typeface.DEFAULT)
                .setTextSize(12)
                .apply(); // required
    }

    public TokenInfo getTokenInfo() {
        return tokenInfo;
    }

    public void setTokenInfo(TokenInfo tokenInfo) {
        this.tokenInfo = tokenInfo;
    }

    public UserInfo getUserInfo() {
        return userInfo;
    }

    public void setUserInfo(UserInfo userInfo) {
        this.userInfo = userInfo;
    }

    public static MyApplication getApplication() {
        return application;
    }

    public List<Activity> getOpenActivityList() {
        return openActivityList;
    }

    public List<Activity> getOpenFilmSelectBookActivityList() {
        return openFilmSelectBookActivityList;
    }

    /**
     * 关闭所有的activity
     */
    public void finishAllActivity() {
        List<Activity> openActivityList = this.getOpenActivityList();
        for (Activity a : openActivityList) {
            a.finish();
        }
    }

    /**
     * 关闭所有的选座activity
     */
    public void finishAllFilmSelectBookActivityList() {
        List<Activity> openActivityList = this.getOpenFilmSelectBookActivityList();
        for (Activity a : openActivityList) {
            a.finish();
        }
    }


    //static 代码段可以防止内存泄露
    static {
        //设置全局的Header构建器
        SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                //全局设置主题颜色
                layout.setPrimaryColorsId(R.color.white, R.color.gray);
                //.setTimeFormat(new DynamicTimeFormat("更新于 %s"));//指定为经典Header，默认是 贝塞尔雷达Header
                return new ClassicsHeader(context);
            }
        });

        //设置全局的Footer构建器

        SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setDrawableSize(1);
            }
        });

    }
}
